﻿var name;
var url;
function aj(name,url){
  var req;

  if (window.XMLHttpRequest){
   req = new XMLHttpRequest();
  }else if (window.ActiveXObject)
  {
   req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if(req)
  {

   req.open("GET","./seturl/setcookie.php?name="+name+"&url="+url,true);

   req.send(null); 
  }
}


function del() {
        target = window.event.srcElement;
        div = target.parentNode;
//get name and url
        name = div.firstChild.innerHTML;
        url = div.firstChild.getAttribute('href');
//remove cookie
        var req;
        if (window.XMLHttpRequest){
          req = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
          req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(req)
        {
        req.open("GET","./seturl/delcookie.php?name="+name+"&url="+url,true);
        req.send(null);
        }
//remove index
        frontlocation = document.getElementsByTagName("a");
        for(i=0;i<frontlocation.length;i++)
        {
          if(frontlocation[i].getAttribute('href') == url)
          {
            rmnode = frontlocation[i].parentNode;
            break;
          }
        }
        rmnode.parentNode.removeChild(rmnode);
//remove this page
        div.parentNode.removeChild(div);
}

function add(){
	
var name = document.getElementById("name").value;
var url = document.getElementById("url").value;
var list = document.getElementById("list");
var div = document.createElement("li");
var frontlocation = document.getElementById("setcookie");//add by peng 2012-10-24
//动态创建节点
div.innerHTML = '<span href="'+url+'">'+name+'</span><input type="button" onclick="del();">';
//aj(name,url);
var link = document.createElement("span");

		if(name=="请输入网站名称")
	{
		alert("请输入网站名称");
		//$("#name").focus();
		return false;
		}
	if(url=="请输入网址")
	{
		alert("请输入网址");
		//$("#url").focus();
		return false;
		}
link.setAttribute("href",url);  
//设置属性		
link.innerHTML = name;				
//增加删除的按钮			
var button = document.createElement("input");	
button.setAttribute("type","button");			
button.value = "  ";	
button.setAttribute("onclick","del();");//add by liujh 2012-12-28
/*  delete by liujh 2012-12-28
button.onclick = function(event){				
var target;				
if (event == null)
{					
		target = window.event.srcElement;

		}else{					
		target = event.target;
		}				
		var div = target.parentNode;
		div.parentNode.removeChild(div);				
			}
*/
//添加生成的内容			
x = list.innerHTML;
	if(x.indexOf(url)<1){
		var div = document.createElement("li");
		div.appendChild(button);		
		div.insertBefore(link,button);			
		list.appendChild(div);

		newNode = document.createElement("div");
		newNode.innerHTML = "<a href="+url+">"+name+"</a>";
		frontlocation.appendChild(newNode);
		aj(name,url)
}	

		}
function aci(x,y){
var name = x;
var urls = y;
var list = document.getElementById("list");

var frontlocation = document.getElementById("setcookie");//add by peng 2012-10-24
var div = document.createElement("li");

div.innerHTML = '<span href="'+urls+'">'+name+'</span><input type="button" onclick="del();">';

x = list.innerHTML;
		if(x.indexOf(urls)<0)
		{
			list.appendChild(div);

		
			newNode = document.createElement("div");//add by peng 2012-10-24
			newNode.innerHTML = '<a href="'+urls+'">'+name+'</a>'; //add by peng 2012-10-24
			frontlocation.appendChild(newNode); //add by peng 2012-10-24
//alert(div.innerHTML);
			aj(name,urls);
		}
}

function dell(){
       target = window.event.srcElement;
        div = target.parentNode;
//get name and url
        name = div.firstChild.innerHTML;
        url = div.firstChild.getAttribute('href');
//remove this page
        div.parentNode.removeChild(div);

}


function dhsql(){
        //var pre;
       /* if(window.XMLHttpRequest){
                pre = new XMLHttpRequest();
        }else if(window.ActiveXObject){
                pre = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(pre){
                pre.open("GET","./seturl/action.php",true);
                pre.send(null);
        }*/
	var xmlHttp;
	if(window.XMLHttpRequest){
                xmlHttp = new XMLHttpRequest();
	}else{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlHttp.onreadystatechange = function(){
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
				if(xmlHttp.responseText == true){
					alert("您的网址已永久保存");
				}
				else{
					alert ("您还没有登录，请登录后永久保存");
					//window.parent.$.fancybox-close.close();
					//location = 'index.php';
				}
			}
	}
	xmlHttp.open("GET","./seturl/action.php",true);
	xmlHttp.send(null);
}




/*function addto() {
	if(session["username"]!=null)
 {
alert("您的网址已永久保存:<a href='index.php'></a>");
 }
 else
 {
 alert("您还没有登录，请登录后永久保存:<a href='login.php'></a>");
 } 
	}*/
